define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QT06_ModelSettingService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService', '$timeout',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService, $timeout) {

            this.init = function(option,call_back){
                openDialog(option,call_back)
            };

            function openDialog(option,call_back){
                $mdDialog.dialog(
                    'module/moduleQT/program/QT06/dialog/QT06_ModelSettingService.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/QT06/QT06.css',
                            traceNameList: [],
                            modelName: option.model_name,
                            beforeShown: async () => {
                                dialog.traceNameList = await getTemplate(dialog.modelName);
                                $timeout(() => {
                                    setupList(dialog.traceNameList);
                                });

                                var checklist_position = '.QT06ModelSetting .kmi-layout-page .ps-scrollbar-content';
                                sortablejs.create($(checklist_position)[0], {
                                    ghostClass: 'ghost',
                                    filter: '.undragable',
                                    animation: 150,
                                    // 拖放结束后的回调函数
                                    onEnd: function (evt) {
                                        $timeout(() => {
                                            var newIndex = evt.newIndex;
                                            var oldIndex = evt.oldIndex;
                                            var chooseItem = dialog.traceNameList.splice(oldIndex, 1)[0];
                                            dialog.traceNameList.splice(newIndex, 0, chooseItem);
                                            resort(dialog.traceNameList);
                                        })
                                    }
                                });
                            },
                            init: () => {
                            },
                            itemToggle: (panel_item, index) => {
                                panel_item.SEQ = panel_item.SEQ == 99 ? index + 1 : 99;
                                resort(dialog.traceNameList);
                            },
                            itemUp: (index) => {
                                const temp = dialog.traceNameList.splice(index, 1);
                                dialog.traceNameList.splice(index - 1, 0, ...temp);
                                setupList(dialog.traceNameList);
                                resort(dialog.traceNameList);
                            },
                            itemDown: (index) => {
                                const temp = dialog.traceNameList.splice(index, 1);
                                dialog.traceNameList.splice(index + 1, 0, ...temp);
                                setupList(dialog.traceNameList);
                                resort(dialog.traceNameList);
                            },
                            reset: async () => {
                                dialog.traceNameList = await getDefaultTemplate();
                                setupList(dialog.traceNameList);
                            },
                            back: () => {
                                dialog.hide();
                            },
                            confirm: async () => {
                                dialog.hide();

                                await saveTemplate(dialog.modelName, dialog.traceNameList);
                                for(var i in dialog.traceNameList)
                                {
                                    dialog.traceNameList[i].active = false;
                                }
                                const has_visible = $filter('filter')(dialog.traceNameList, (val) => val.SEQ != 99);
                                has_visible[0].active = 1

                                call_back && call_back(dialog);
                            },
                        };
                    }
                );
            };    

            const service_QTP_Step2 =  (content) => {
                return new Promise(async (resolve, reject) => {
                    let result;
                    $MMWService.sendToServer({
                        uri: "ServicesSTD.Module_LEAN.QTP_Step2",
                        content: content,
                        success: (data) => (result = resolve(data)),
                    });

                    return result;
                });
            };

            const getTemplate = async (modelName) => {
                const content = { Name: "GetTBLRPTTRACEBASIS", ModelName: modelName  };
                const resp = await service_QTP_Step2(content);
                return resp.TRACENAMEList
            }

            const getDefaultTemplate = async () => {
                const content = { Name: "GetIniTraceBasis" };
                const resp = await service_QTP_Step2(content);
                return resp.TRACENAMEList
            }
            
            // 20220719 modify by Alan for#113392 : 儲存模板時，傳入ModelName
            const saveTemplate = async (modelName, list) => {
                const content = { Name: "SaveTemplate", ModelName: modelName , Data: list };
                return await service_QTP_Step2(content);
            }

            // 重新排序SEQ
            // 20220721 modify by Alan for#113392 : SEQ從1開始計算
            const resort = (list) => {
                const has_visible = $filter('filter')(list, (val) => val.SEQ != 99);
                has_visible.forEach((item, index) => {
                    item.SEQ = index + 1;
                });
            }

            const setupList = (list) => {
                for(var i in list) {
                    list[i].show_up = true;
                    list[i].show_down = true;
                }
                list[0].show_up = false;
                list[list.length - 1].show_down = false;
            }

        }
    ])
});
